import { createApp } from 'vue'
import * as VueRouter from 'vue-router';
import App from './App.vue'
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';
import routes from "./router/route";
import * as Icons from '@ant-design/icons-vue';
import STable from '@surely-vue/table';
import ECharts from 'vue-echarts';
import 'echarts';

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    // 允许未登录访问的路径
    const openPaths = ['/login'];

    // 判断目标路径是否为开放路径
    if (openPaths.includes(to.path)) {
        // 如果目标路径是开放路径，直接放行
        next();
    } else {
        // 获取存储在本地的admin对象
        let admin = JSON.parse(window.localStorage.getItem('ZJ-admin'));

        if (!admin) {
            // 如果没有admin对象，跳转到登录页面
            next({ path: '/login' });
        } else {
            // 如果有admin对象，直接放行到目标路径
            next();
        }
    }
});


const app = createApp(App);
app.use(router);
app.use(Antd);
app.component('ECharts', ECharts)
app.use(STable);
app.mount('#app');
const icons: any = Icons;
for (const i in icons) {
    app.component(i, icons[i]);
}
